#include "entity.h"

//Get rid of the sprintf warning outdate
#pragma warning( disable : 4996 )


// Hud Box Functions
HRESULT hudBoxAttack(float top, float bottom, float left, float right);
HRESULT hudBoxMagic(float top, float bottom, float left, float right);
HRESULT hudBoxMove(float top, float bottom, float left, float right);
HRESULT hudBoxBack(float top, float bottom, float left, float right);

// Point New Vertex Buffer
LPDIRECT3DVERTEXBUFFER9 createVertexBuffer(int size, DWORD usage);
LPDIRECT3DVERTEXBUFFER9 g_pVB0        = NULL; // Buffer to hold vertices
LPDIRECT3DVERTEXBUFFER9 g_pVB        = NULL; // Buffer to hold vertices
LPDIRECT3DVERTEXBUFFER9 g_pVB1        = NULL; // Buffer to hold vertices
LPDIRECT3DVERTEXBUFFER9 g_pVB2        = NULL; // Buffer to hold vertices
LPDIRECT3DVERTEXBUFFER9 g_pVB3        = NULL; // Buffer to hold vertices

void drawVB(LPDIRECT3DVERTEXBUFFER9 buffer);
#define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZRHW|D3DFVF_DIFFUSE)
LPDIRECT3DDEVICE9       pd3dDevice;     // the direct3d device

// A structure for our custom vertex type
struct CUSTOMVERTEX
{
    FLOAT x, y, z, rhw;      // The untransformed, 3D position for the vertex
    DWORD color;        // The vertex color
};

//Font pointers
LPD3DXFONT font1;
LPD3DXFONT font3;

//Declare players
Horde orc;
Alliance human;
int damage = 0;

//Declare Turn *Horde = 1, Alliance = 2*
int turn = 1;

//HUD
void DisplayText(LPCSTR text, int number, int team, int xPos, int yPos);
void DiplayTest2(LPCSTR text, int number, int team, int xPos, int yPos);


/*********************************************************************************
        (LPCSTR text, = Like a String "Hello World"
        int number,   = Input a number if displaying one, NULL if not. Can't do both.
        int team,         = Changes color of text 0: Black, 1: Alliance, 2: Horde, 3: Green
                                                                                  4: White
        int xPos,         = X Coord where displaying
        int yPos)         = Y Coord where displaying
**********************************************************************************/
void DisplayText(LPCSTR text, int number, int color, int xPos, int yPos)
{
        D3DCOLOR fontColor;

          if(color == 1)
                        fontColor = D3DCOLOR_ARGB(255,0,0,255);   //Alliance- BLUE
          else if(color == 2)
                        fontColor = D3DCOLOR_ARGB(255,255,0,0);  //Horde- Red
          else if(color == 3)
                        fontColor = D3DCOLOR_ARGB(255,0,255,0);  //GREEN
          else if(color == 4)
                        fontColor = D3DCOLOR_ARGB(255,255,255,255);  //GREEN
          else
          {
                        // Create a color for Monsters - in this case black
                        fontColor = D3DCOLOR_ARGB(255,0,0,0); 
          }

      // Create a rectangle to indicate where on the screen it should be drawn
      RECT rct;
      rct.left=xPos;
      rct.right= rct.left + 120;
      rct.top=yPos;
      rct.bottom=rct.top+20;

          if(number != NULL)
          {
                 char outstring[50];
                 sprintf(outstring, "%d" , number);
                 font1->DrawText(NULL, outstring, -1, &rct, DT_NOCLIP, fontColor); 
          }
          else
          {
                // Draw some text
                font1->DrawText(NULL, text , -1, &rct, DT_NOCLIP, fontColor );
          }
}

//Display on bigger font
void DisplayText2(LPCSTR text, int number, int color, int xPos, int yPos)
{
        D3DCOLOR fontColor;

          if(color == 1)
                        fontColor = D3DCOLOR_ARGB(255,0,0,255);   //Alliance- BLUE
          else if(color == 2)
                        fontColor = D3DCOLOR_ARGB(255,255,0,0);  //Horde- Red
          else if(color == 3)
                        fontColor = D3DCOLOR_ARGB(255,0,255,0);  //GREEN
          else if(color == 4)
                        fontColor = D3DCOLOR_ARGB(255,255,255,255);  //GREEN
          else
          {
                        // Create a color for Monsters - in this case black
                        fontColor = D3DCOLOR_ARGB(255,0,0,0); 
          }

      // Create a rectangle to indicate where on the screen it should be drawn
      RECT rct;
      rct.left=xPos;
      rct.right= rct.left + 120;
      rct.top=yPos;
      rct.bottom=rct.top+20;

          if(number != NULL)
          {
                 char outstring[50];
                 sprintf(outstring, "%d" , number);
                 font3->DrawText(NULL, outstring, -1, &rct, DT_NOCLIP, fontColor); 
          }
          else
          {
                 // Draw some text
                 font3->DrawText(NULL, text , -1, &rct, DT_NOCLIP, fontColor );
          }
}


//Sets Camera to Ortho to display 2d HUD
void SetHudCamera(float WindowWidth, float WindowHeight)
{
        D3DXMATRIX Ortho2D;
        D3DXMATRIX Identity;

        D3DXMatrixOrthoLH(&Ortho2D, WindowWidth, WindowHeight, 0.0f, 1.0f);
        D3DXMatrixIdentity(&Identity);

        pd3dDevice->SetTransform(D3DTS_PROJECTION, &Ortho2D);
        pd3dDevice->SetTransform(D3DTS_WORLD, &Identity);
        pd3dDevice->SetTransform(D3DTS_VIEW, &Identity);
}


//Couldnt get graphics yet, prof working on it and will email it to us.
//Also working on it. -TJD
void drawHud()
{
		if(turn == 1)
		{
			DisplayText("Player 2: HORDE", NULL, 2, 240, 3); 
			//HORDE
			DisplayText(orc.getPlayer(), NULL, 2, 60, 440);
		
			DisplayText("Health:", NULL, 0, 60 , 460);
			DisplayText(     NULL, orc.getHealth(), 2, 120, 460);

			//Attack/Defense
			DisplayText(" Attack =", NULL, 0, 180, 440);
			DisplayText(      NULL, orc.getAttack(), 2, 255, 440);
			DisplayText("Defense =", NULL, 0, 173, 460);
			DisplayText(      NULL, orc.getDefense(), 2, 255, 460);

			//Magic/Resist
			DisplayText("  Magic =", NULL, 0, 280, 440);
			DisplayText(      NULL, orc.getMagic(), 2,  355, 440);
			DisplayText(" Resist =", NULL, 0, 280, 460);
			DisplayText(      NULL, orc.getResist(), 2,  355, 460);
		}

		if(turn == 2)
		{
			DisplayText("Player 1: ALLIANCE", NULL, 1, 240, 3); 
			//ALLIANCE
			DisplayText(human.getPlayer(), NULL, 1, 60, 440);

			DisplayText("Health:", NULL, 0, 60 , 460);
			DisplayText(     NULL, human.getHealth(), 1, 120, 460);

			//Attack/Defense
			DisplayText(" Attack =", NULL, 0, 180, 440);
			DisplayText(      NULL, human.getAttack(), 1, 255, 440);
			DisplayText("Defense =", NULL, 0, 173, 460);
			DisplayText(      NULL, human.getDefense(), 1, 255, 460);

			//Magic/Resist
			DisplayText("  Magic =", NULL, 0, 280, 440);
			DisplayText(      NULL, human.getMagic(), 1,  355, 440);
			DisplayText(" Resist =", NULL, 0, 280, 460);
			DisplayText(      NULL, human.getResist(), 1,  355, 460);
		}

        //Attack/Magic Button
        DisplayText("1) Attack", NULL, 4, 400, 440);
        DisplayText("2) Magic ", NULL, 4, 400, 460);
		DisplayText("3) Move ", NULL, 4, 500, 450);
		

		//TEHEHE EasterEgg ftw
		//DisplayText("TyToons", NULL,4, 500, 460);

}

//ADD 
#define NUM_VERTICES	20

HRESULT hudBoxAttack(float top, float bottom, float left, float right)
{
	HRESULT hr;

    // Initialize three vertices for rendering a triangle
    CUSTOMVERTEX g_Vertices[] =
    {
        { left,  top, 0.5f, 1.0f, D3DCOLOR_ARGB(0,0,0,0),}, // x, y, z, rhw, color
        { right, top, 0.5f, 1.0f, D3DCOLOR_ARGB(0,0,0,0),},
        { left,  bottom, 0.5f, 1.0f, D3DCOLOR_ARGB(0,0,0,0),},
        { right, bottom, 0.5f, 1.0f, D3DCOLOR_ARGB(0,0,0,0),}
	};

	// Create the vertex buffer
	g_pVB = createVertexBuffer(NUM_VERTICES *sizeof(CUSTOMVERTEX), D3DFVF_CUSTOMVERTEX);

    // Fill the vertex buffer.
    VOID* pVertices;
	
	hr = g_pVB->Lock( 0, sizeof(g_Vertices), (void**)&pVertices, 0 );
	if FAILED (hr)
        return E_FAIL;

	// copy the vertices into the buffer
    memcpy( pVertices, g_Vertices, sizeof(g_Vertices) );

	// unlock the vertex buffer
    g_pVB->Unlock();

    return S_OK;
}

HRESULT hudBoxMagic(float top, float bottom, float left, float right)
{
	HRESULT hr;

    // Initialize three vertices for rendering a triangle
    CUSTOMVERTEX g_Vertices[] =
    {
        { left,  top, 0.5f, 1.0f, D3DCOLOR_ARGB(0,0,0,0),}, // x, y, z, rhw, color
        { right, top, 0.5f, 1.0f, D3DCOLOR_ARGB(0,0,0,0),},
        { left,  bottom, 0.5f, 1.0f, D3DCOLOR_ARGB(0,0,0,0),},
        { right, bottom, 0.5f, 1.0f, D3DCOLOR_ARGB(0,0,0,0),}
	};

	// Create the vertex buffer
	g_pVB1 = createVertexBuffer(NUM_VERTICES *sizeof(CUSTOMVERTEX), D3DFVF_CUSTOMVERTEX);

    // Fill the vertex buffer.
    VOID* pVertices;
	
	hr = g_pVB1->Lock( 0, sizeof(g_Vertices), (void**)&pVertices, 0 );
	if FAILED (hr)
        return E_FAIL;

	// copy the vertices into the buffer
    memcpy( pVertices, g_Vertices, sizeof(g_Vertices) );

	// unlock the vertex buffer
    g_pVB1->Unlock();

    return S_OK;
}

HRESULT hudBoxMove(float top, float bottom, float left, float right)
{
	HRESULT hr;

    // Initialize three vertices for rendering a triangle
    CUSTOMVERTEX g_Vertices[] =
    {
        { left,  top, 0.5f, 1.0f, D3DCOLOR_ARGB(0,0,0,0),}, // x, y, z, rhw, color
        { right, top, 0.5f, 1.0f, D3DCOLOR_ARGB(0,0,0,0),},
        { left,  bottom, 0.5f, 1.0f, D3DCOLOR_ARGB(0,0,0,0),},
        { right, bottom, 0.5f, 1.0f, D3DCOLOR_ARGB(0,0,0,0),}
	};

	// Create the vertex buffer
	g_pVB2 = createVertexBuffer(NUM_VERTICES *sizeof(CUSTOMVERTEX), D3DFVF_CUSTOMVERTEX);

    // Fill the vertex buffer.
    VOID* pVertices;
	
	hr = g_pVB2->Lock( 0, sizeof(g_Vertices), (void**)&pVertices, 0 );
	if FAILED (hr)
        return E_FAIL;

	// copy the vertices into the buffer
    memcpy( pVertices, g_Vertices, sizeof(g_Vertices) );

	// unlock the vertex buffer
    g_pVB2->Unlock();

    return S_OK;
}

HRESULT hudBoxBack(float top, float bottom, float left, float right)
{
	HRESULT hr;

    // Initialize three vertices for rendering a triangle
    CUSTOMVERTEX g_Vertices[] =
    {
        { left,  top, 0.5f, 1.0f, D3DCOLOR_ARGB(0,71,71,71),}, // x, y, z, rhw, color
        { right, top, 0.5f, 1.0f, D3DCOLOR_ARGB(0,71,71,71),},
        { left,  bottom, 0.5f, 1.0f, D3DCOLOR_ARGB(0,71,71,71),},
        { right, bottom, 0.5f, 1.0f, D3DCOLOR_ARGB(0,71,71,71),},
	};

	// Create the vertex buffer
	g_pVB0 = createVertexBuffer(NUM_VERTICES *sizeof(CUSTOMVERTEX), D3DFVF_CUSTOMVERTEX);

    // Fill the vertex buffer.
    VOID* pVertices;
	
	hr = g_pVB0->Lock( 0, sizeof(g_Vertices), (void**)&pVertices, 0 );
	if FAILED (hr)
        return E_FAIL;

	// copy the vertices into the buffer
    memcpy( pVertices, g_Vertices, sizeof(g_Vertices) );

	// unlock the vertex buffer
    g_pVB0->Unlock();

    return S_OK;
}

HRESULT hudBoxBack1(float top, float bottom, float left, float right)
{
	HRESULT hr;

    // Initialize three vertices for rendering a triangle
    CUSTOMVERTEX g_Vertices[] =
    {
        { left,  top, 0.5f, 1.0f, D3DCOLOR_ARGB(0,0,0,0),}, // x, y, z, rhw, color
        { right, top, 0.5f, 1.0f, D3DCOLOR_ARGB(0,0,0,0),},
        { left,  bottom, 0.5f, 1.0f, D3DCOLOR_ARGB(0,0,0,0),},
        { right, bottom, 0.5f, 1.0f, D3DCOLOR_ARGB(0,0,0,0),}
	};

	// Create the vertex buffer
	g_pVB3 = createVertexBuffer(NUM_VERTICES *sizeof(CUSTOMVERTEX), D3DFVF_CUSTOMVERTEX);

    // Fill the vertex buffer.
    VOID* pVertices;
	
	hr = g_pVB3->Lock( 0, sizeof(g_Vertices), (void**)&pVertices, 0 );
	if FAILED (hr)
        return E_FAIL;

	// copy the vertices into the buffer
    memcpy( pVertices, g_Vertices, sizeof(g_Vertices) );

	// unlock the vertex buffer
    g_pVB3->Unlock();

    return S_OK;
}


LPDIRECT3DVERTEXBUFFER9 createVertexBuffer(int size, DWORD usage)
{
	HRESULT hr;
	LPDIRECT3DVERTEXBUFFER9 buffer;

    // Create the vertex buffer.
    hr = pd3dDevice->CreateVertexBuffer( size,
                                         0, 
										 usage,
                                         D3DPOOL_DEFAULT, 
										 &buffer, 
										 NULL );
	if FAILED ( hr)
        return NULL;
    
	return buffer;
}

void drawVB(LPDIRECT3DVERTEXBUFFER9 buffer)
{
	pd3dDevice->SetStreamSource( 0, buffer, 0, sizeof(CUSTOMVERTEX) );
    pd3dDevice->SetFVF( D3DFVF_CUSTOMVERTEX );
    pd3dDevice->DrawPrimitive( D3DPT_TRIANGLESTRIP, 0, 1 );
    pd3dDevice->DrawPrimitive( D3DPT_TRIANGLESTRIP, 0, 2 );
    //pd3dDevice->DrawPrimitive( D3DPT_TRIANGLEFAN, 2, 4 );
}

void drawHCombatLog()
{
	int damage = 0;
	if((orc.getAttackDamage() > human.getArmorLevel()))
		damage = (orc.getAttackDamage() - human.getArmorLevel());
	else
		damage = 0;
	if(orc.getCrit() == true)
	{
		int space = 80;
		DisplayText(orc.getPlayer(), NULL, 0, 100+space, 100);
		DisplayText("deals", NULL, 0, 145+space, 100);
		DisplayText(NULL, damage, 0, 190+space, 100);
		DisplayText("critical damage to", NULL, 0, 205+space, 100);
		DisplayText(human.getPlayer(), NULL, 0, 350+space, 100);
	}
	else
	{
		int space = 100;
		DisplayText(orc.getPlayer(), NULL, 0, 100+space, 100);
		DisplayText("deals", NULL, 0, 145+space, 100);
		DisplayText(NULL, damage, 0, 190+space, 100);
		DisplayText("damage to", NULL, 0, 205+space, 100);
		DisplayText(human.getPlayer(), NULL, 0, 290+space, 100);
	}	
}

void drawACombatLog()
{
	int damage = 0; 
	if((human.getAttackDamage() > orc.getArmorLevel()))
		damage = (human.getAttackDamage() - orc.getArmorLevel());
	else
		damage = 0;

		if(human.getCrit() == true)
		{
			int space = 80;
			DisplayText(human.getPlayer(), NULL, 0, 100+space, 100);
			DisplayText("deals", NULL, 0, 150+space, 100);
			DisplayText(NULL, damage, 0, 195+space, 100);
			DisplayText("critical damage to", NULL, 0, 210+space, 100);
			DisplayText(orc.getPlayer(), NULL, 0, 355+space, 100);
		}
		else
		{
			int space = 100;
			DisplayText(human.getPlayer(), NULL, 0, 100+space, 100);
			DisplayText("deals", NULL, 0, 150+space, 100);
			DisplayText(NULL, damage, 0, 195+space, 100);
			DisplayText("damage to", NULL, 0, 210+space, 100);
			DisplayText(orc.getPlayer(), NULL, 0, 295+space, 100);
		}

	
}